// 导入request请求工具类
import { 
    getBaseUrl,
    getWxLogin,
    getUserProfile,
    requestPay,
     requestUtil
} from '../../utils/requestUtil.js';
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({ 
    /**
     * 页面的初始数据
     */
     
    data: {  
        baseUrl: '',
          cart: [],
          address: {},
          totalPrice: 0,
          totalNum: 0,
        status: 0
    },
     
    /**
     * 生命周期函数--监听页面加载
     */
     onLoad: function(options) {  
        const baseUrl = getBaseUrl();  
        this.setData({    baseUrl  })
    },
    async  handleOrderPay() {
        // wx.login({
        //      timeout: 5000,
        //      success:(res)=>{
        //       console.log("code="+res.code)
        //     }
        //    })
        // let res = await getWxLogin();
        // console.log("code=" + res.code)
        // wx.getUserProfile({   
        //     desc: '获取用户信息',
        //        success: (res) => {    
        //         console.log(res.userInfo.nickName, res.userInfo.avatarUrl)  
        //     } 
        // })
        // let res2 = await getUserProfile();
        // console.log(res2.userInfo.nickName, res2.userInfo.avatarUrl)

        const token = wx.getStorageSync('token');
        if (!token) {
            Promise.all([getWxLogin(), getUserProfile()]).then((res) => {   
                console.log(res[0].code)   
                console.log(res[1].userInfo.nickName, res[1].userInfo.avatarUrl) 
                let loginParam = {
                    code: res[0].code,
                    nickName: res[1].userInfo.nickName,
                    avatarUrl: res[1].userInfo.avatarUrl
                }
                console.log(loginParam)
                wx.setStorageSync('userInfo', res[1].userInfo);
                this.wxlogin(loginParam)
            })
        } else {
            wx.showModal({    
                title: '友情提示',
                    content: '您即将要支付',
                  success: async  res => {     
                    // Promise.all([getWxLogin(), getUserProfile()]).then((res) => {      
                    //     console.log(res[0].code)      
                    //     console.log(res[1].userInfo.nickName, res[1].userInfo.avatarUrl)     
                    //     let loginParam = {        code: res[0].code,        nickName: res[1].userInfo.nickName,        avatarUrl: res[1].userInfo.avatarUrl      }      
                    //     console.log(loginParam)       // 把用户信息放到缓存中
                    //     wx.setStorageSync('userInfo', res[1].userInfo);      
                    //     this.wxlogin(loginParam);      
                    //     this.setData({        userInfo: res[1].userInfo      })    
                    // })   
                    if (res.cancel) {
                        const totalPrice = this.data.totalPrice; // 请求体 总价
                          
                        const address = this.data.address.provinceName + this.data.address.cityName + this.data.address.countyName + this.data.address.detailInfo; // 请求体 收货地址
                          
                        const consignee = this.data.address.userName; // 请求体 收货人
                          
                        const telNumber = this.data.address.telNumber; // 请求体 联系电话
                        const status = this.data.status = 1;  
                        let goods = [];  
                        this.data.cart.forEach(v => goods.push({   
                            goodsId: v.id,
                              goodsNumber: v.num,
                             goodsPrice: v.price,
                               
                            goodsName: v.name,
                               goodsPic: v.proPic 
                        }))  
                        const orderParam = {   
                            totalPrice,
                               address,
                               status,
                            consignee,
                               telNumber,
                               goods
                        } 
                        const res = await requestUtil({ url: "/my/order/create", method: "POST", data: orderParam });  
                        console.log(res.orderNo);
                        wx.showToast({    
                            title: '已取消',
                                icon: 'none'  
                        })   
                        setTimeout(function() {
                            wx.navigateTo({
                                url: "/pages/order/index?type=1"
                            });
                        }, 800);
                    } else {
                        wx.showToast({
                            title: '支付成功',
                            icon: 'none'
                        })
                        const totalPrice = this.data.totalPrice; // 请求体 总价
                          
                        const address = this.data.address.provinceName + this.data.address.cityName + this.data.address.countyName + this.data.address.detailInfo; // 请求体 收货地址
                          
                        const consignee = this.data.address.userName; // 请求体 收货人
                          
                        const telNumber = this.data.address.telNumber; // 请求体 联系电话
                        const status = this.data.status = 2;  
                        let goods = [];  
                        this.data.cart.forEach(v => goods.push({   
                            goodsId: v.id,
                              goodsNumber: v.num,
                             goodsPrice: v.price,
                               
                            goodsName: v.name,
                               goodsPic: v.proPic 
                        }))  
                        const orderParam = {   
                            totalPrice,
                               address,
                               status,
                            consignee,
                               telNumber,
                               goods
                        } 
                        const res = await requestUtil({ url: "/my/order/create", method: "POST", data: orderParam });  
                        console.log(res.orderNo);



                        // 删除缓存中的 已经支付的商品
                           
                        let newCart = wx.getStorageSync('cart');   
                        newCart = newCart.filter(v => !v.checked);   
                        wx.setStorageSync('cart', newCart);   
                        wx.showToast({    
                            title: '支付成功',
                                icon: 'none'  
                        })   
                        setTimeout(function() {

                            wx.navigateTo({

                                url: "/pages/order/index?type=2"

                            });

                        }, 800);
                    }
                }  

            }) 


        }


    },

    /**
     * 请求后端获取用户token
     * @param {} loginParam
     */
     async wxlogin(loginParam) {  
        const result = await
        requestUtil({ url: "/user/wxlogin", data: loginParam, method: "post" });  
        console.log(result);
        const token = result.token;
        if (result.code === 0) {
            wx.setStorageSync('token', token);
            console.log("支付继续走，创建订单");
            this.handleOrderPay();
        }
    },

    /**
     * 生命周期函数--监听页面显示
     */
     onShow: function() {  
        console.log("show")  
        const address = wx.getStorageSync('address');  
        let cart = wx.getStorageSync('cart') || [];   // 选中的商品显示
          
        cart = cart.filter(v => v.checked);  
        let totalPrice = 0;  
        let totalNum = 0;  
        cart.forEach(v => {    
            totalPrice += v.num * v.price;    
            totalNum += v.num; 
        })  
        this.setData({    cart,    address,    totalNum,    totalPrice  })
    }
})